#include <sys/types.h>
#include <sys/sem.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

union semun {
    int val;
    struct semid_ds *buf;
    unsigned short  *array;
} arg;

int main(int argc, char const *argv[])
{
    key_t key;
    int semid, semval;
    union  semun semopts;
    if  ((key = ftok(".", 's')) == -1) {
        perror("ftof error\n");
        exit(1);
    }
    if ((semid = semget(key, 1, IPC_CREAT|0666)) == -1) {
        perror("semget error!\n");
        exit(1);
    }
    while (1) {
        if ((semval = semctl(semid, 0, GETVAL, 0)) == -1) {
            perror("semctl error!\n");
            exit(1);
        }
        if (semval > 0) {
            printf("Stil %d resources can be used\n", semval);

        } else
        {
            printf("No more resources can be used!\n");
        }
        sleep(3);
        
    }
    
    return 0;
}
